package com.skyc.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.google.gson.Gson;
import com.skyc.entity.Clothes;
import com.skyc.entity.User;
import com.skyc.service.ClothesService;
import com.skyc.service.ShopService;
import com.skyc.service.UserService;
import com.skyc.service.impl.ClothesServiceImpl;
import com.skyc.service.impl.ShopServicImpl;

/**
 * Servlet implementation class AServlet
 */
@WebServlet("/CategoryServlet")
public class CategoryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	UserService us = null;
	ClothesService cs = null;
	PrintWriter out = null;
	HttpSession session = null;
	User user = null;
	Gson gson = null;
	String[] aa = { "cate_sex", "cate_type", "cate_color", "cate_size", "cate_priceMin", "cate_priceMax", "cate_page" };

	protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
		doPost(req, response);

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// request.setCharacterEncoding("utf-8");
		// response.setContentType("text/html;charset=utf-8");
		// response.setCharacterEncoding("utf-8");
		session = request.getSession();
		// ServletContext application=getServletContext();
		String flag = request.getParameter("flag");// 来源
		out = response.getWriter();
		cs = new ClothesServiceImpl();
		user = (User) session.getAttribute("user");
		gson = new Gson();
		for (int i = 0; i < 4; i++) {
			if (session.getAttribute(aa[4]) == null) {
				session.setAttribute(aa[i], "");
			}
		}
		if (session.getAttribute(aa[4]) == null) {
			session.setAttribute(aa[4], "0");
		}
		if (session.getAttribute(aa[5]) == null) {
			session.setAttribute(aa[5], "100000");
		}
		if (flag != null) {
			switch (flag) {
			case "page":
				doPage(request, response);// 跳转页数
				break;
			case "query":
				doQuery(request, response);// 条件查询
				break;
			case "deleteall":
				doDeleteall(request, response);// 重置搜索
				break;
			case "downup":
				doDownUp(request, response);// 上一页，下一页
				break;
			case "from":
				doFrom(request, response);// 其他界面进来
				return;
			case "index":
				doIndex(request, response);// 其他界面进来
				break;
			default:
				break;
			}
		}
		if (session.getAttribute("catelistsnum") == null) {
			session.setAttribute("catelistsnum", 0);
		}
		out.flush();
		out.close();
	}

	private void charUtil(char[] strs, String a[]) {
		for (int i = 0; i < strs.length; i++) {
			if ('男' == strs[i] || '女' == strs[i]) {
				a[0] = strs[i] + "";
			}
			if ('上' == strs[i] || '下' == strs[i] || '裙' == strs[i] || '套' == strs[i]) {
				a[1] = strs[i] + "";
			}
			if ('红' == strs[i] || '粉' == strs[i] || '蓝' == strs[i] || '白' == strs[i] || '黑' == strs[i] || '黄' == strs[i]
					|| '紫' == strs[i]) {
				a[2] = strs[i] + "";
			}

		}
	}

	private void doFrom(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String str = request.getParameter("cate_key");
		List<Clothes> lists = null;
		String ab[] = { "", "", "", "", "", "", "" };
		int size = 0;
		if (str == null) {
			ab[0] = request.getParameter("csex");
			ab[1] = request.getParameter("ctype");

		} else {
			char[] strs = str.toCharArray();
			charUtil(strs, ab);

		}
		lists = cs.queryPriceAsc(ab[0], ab[1], ab[2], "", "", "", 0 + "", 25 + "");
		size = cs.queryTermAll(ab[0] + "", ab[1] + "", "", "", "", "").size();

		session.setAttribute("catelistsnum", size);
		int pages = size / 25 + 1;
		if (pages == 0 && size % 25 == 0) {
			pages = pages - 1;
		}

		session.setAttribute("catepagesum", pages);
		session.setAttribute("catelists", lists);
		setImgUtil(lists, request, response);
		response.sendRedirect("category.jsp");
		if (session.getAttribute("catelistsnum") == null) {
			session.setAttribute("catelistsnum", 0);
		}
		out.flush();
		out.close();
	}

	private void doPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String bb[] = new String[aa.length];
		for (int i = 0; i < 6; i++) {
			bb[i] = session.getAttribute(aa[i]).toString();
		}
		bb[6] = request.getParameter(aa[6]);
		int pagenum = 1;
		if (bb[6] != null && !"".equals(bb[6])) {
			pagenum = Integer.valueOf(bb[6]);
		}
		List<Clothes> lists = cs.queryPriceAsc(bb[0], bb[1], bb[2], bb[3], bb[4], bb[5], (pagenum - 1) * 25 + "",
				25 + "");
		int size = cs.queryTermAll(bb[0], bb[1], bb[2], bb[3], bb[4], bb[5]).size();
		session.setAttribute("catelistsnum", size);
		int pages = size / 25 + 1;
		if (pages == 0 && size % 25 == 0) {
			pages = pages - 1;
		}

		session.setAttribute("catepagesum", pages);
		session.setAttribute("catelists", lists);
		setImgUtil(lists, request, response);
		request.getRequestDispatcher("category_showdiv.jsp").forward(request, response);

	}

	// 价格、销量的升序降序
	private void doDownUp(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String value = request.getParameter("value1");
		String type = request.getParameter("type1");
		if (value != null) {
			String bb[] = new String[aa.length];
			for (int i = 0; i < aa.length; i++) {
				bb[i] = session.getAttribute(aa[i]).toString();
			}
			List<Clothes> catelists = null;
			switch (value) {
			case "升序":
				if ("xl".equals(type)) {
					int pagenum = 1;
					if (bb[6] != null && !"".equals(bb[6])) {
						pagenum = Integer.valueOf(bb[6]);
					}
					catelists = cs.querySalesAsc(bb[0], bb[1], bb[2], bb[3], bb[4], bb[5], (pagenum - 1) * 25 + "",
							25 + "");
				}
				if ("jg".equals(type)) {
					int pagenum = 1;
					if (bb[6] != null && !"".equals(bb[6])) {
						pagenum = Integer.valueOf(bb[6]);
					}
					catelists = cs.queryPriceAsc(bb[0], bb[1], bb[2], bb[3], bb[4], bb[5], (pagenum - 1) * 25 + "",
							25 + "");
				}
				break;
			case "降序":
				if ("xl".equals(type)) {
					int pagenum = 1;
					if (bb[6] != null && !"".equals(bb[6])) {
						pagenum = Integer.valueOf(bb[6]);
					}
					catelists = cs.querySalesDesc(bb[0], bb[1], bb[2], bb[3], bb[4], bb[5], (pagenum - 1) * 25 + "",
							25 + "");
				}
				if ("jg".equals(type)) {
					int pagenum = 1;
					if (bb[6] != null && !"".equals(bb[6])) {
						pagenum = Integer.valueOf(bb[6]);
					}
					catelists = cs.queryPriceDesc(bb[0], bb[1], bb[2], bb[3], bb[4], bb[5], (pagenum - 1) * 25 + "",
							25 + "");
				}
				break;
			default:
				break;
			}
			int pagenum = 1;
			if (bb[6] != null && !"".equals(bb[6])) {
				pagenum = Integer.valueOf(bb[6]);
			}
			List<Clothes> lists = cs.queryPriceAsc(bb[0], bb[1], bb[2], bb[3], bb[4], bb[5], (pagenum - 1) * 25 + "",
					25 + "");
			int size = cs.queryTermAll(bb[0], bb[1], bb[2], bb[3], bb[4], bb[5]).size();
			session.setAttribute("catelistsnum", size);// 总数据
			int pages = size / 25 + 1;
			if (pages == 0 && size % 25 == 0) {
				pages = pages - 1;
			}
			session.setAttribute("catepagesum", pages);// 显示25条数据
			session.setAttribute("catelists", catelists);// 第几页商品集合
			setImgUtil(catelists, request, response);
			request.getRequestDispatcher("category_showdiv.jsp").forward(request, response);
		}
	}

	private void deleteUtil(HttpServletRequest request, HttpServletResponse response) {
		String bb[] = new String[aa.length];
		for (int i = 0; i < aa.length; i++) {
			session.setAttribute(aa[i], "");
			bb[i] = session.getAttribute(aa[i]).toString();
		}
		int pagenum = 1;
		if (bb[6] != null && !"".equals(bb[6])) {
			pagenum = Integer.valueOf(bb[6]);
		}
		List<Clothes> lists = cs.queryPriceAsc(bb[0], bb[1], bb[2], bb[3], bb[4], bb[5], (pagenum - 1) * 25 + "",
				25 + "");
		int size = cs.queryTermAll(bb[0], bb[1], bb[2], bb[3], bb[4], bb[5]).size();
		session.setAttribute("catelistsnum", size);// 显示25条数据
		int pages = size / 25 + 1;
		if (pages == 0 && size % 25 == 0) {
			pages = pages - 1;
		}
		session.setAttribute("catepagesum", pages);// 页数
		session.setAttribute("catelists", lists);
		setImgUtil(lists, request, response);
	}

	private void doIndex(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		deleteUtil(request, response);
		response.sendRedirect("category.jsp");
	}

	private void doDeleteall(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		deleteUtil(request, response);
		request.getRequestDispatcher("category_showdiv.jsp").forward(request, response);

	}

	private void setImgUtil(List<Clothes> catelists, HttpServletRequest request, HttpServletResponse response) {
		if (catelists != null) {
			String[] cate_img = new String[catelists.size()];
			for (int i = 0; i < catelists.size(); i++) {
				String[] cc = catelists.get(i).getCimgurl().split("/");
				if (cc != null || cc.length != 0) {
					cate_img[i] = cc[1];// 用分号切割取第二张图
				}

			}
			session.setAttribute("cate_img", cate_img);
		}
	}

	private void doQuery(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String cc = "cate_" + request.getParameter("type1");
		String value1 = request.getParameter("value1");
		session.setAttribute(cc, value1);
		String dd = "cate_" + request.getParameter("type2");
		String value2 = request.getParameter("value2");
		session.setAttribute(dd, value2);
		String bb[] = new String[aa.length];
		for (int i = 0; i < aa.length - 1; i++) {
			bb[i] = session.getAttribute(aa[i]).toString();
		}
		int pagenum = 1;
		if (bb[6] != null && !"".equals(bb[6])) {
			pagenum = Integer.valueOf(bb[6]);
		}
		List<Clothes> lists = cs.queryPriceAsc(bb[0], bb[1], bb[2], bb[3], bb[4], bb[5], (pagenum - 1) * 25 + "",
				25 + "");
		int size = cs.queryTermAll(bb[0], bb[1], bb[2], bb[3], bb[4], bb[5]).size();
		session.setAttribute("catelistsnum", size);
		int pages = size / 25 + 1;
		if (pages == 0 && size % 25 == 0) {
			pages = pages - 1;
		}
		session.setAttribute("catepagesum", pages);
		session.setAttribute("catelists", lists);
		setImgUtil(lists, request, response);
		request.getRequestDispatcher("category_showdiv.jsp").forward(request, response);
		// out.print(lists);
	}

}
